<?php
/**
 * Created by PhpStorm.
 * User: chenyu
 * Date: 2017-09-11
 * Time: 13:59
 */

namespace EpgApp\modules\sdks\Image\base;

use EpgApp\modules\sdks\Image\base\gbclass\ImageSDK ;
use EpgApp\modules\sdks\Image\base\Imagickclass\ImagickSDK ;


class ImageCreator
{
        private static $_creator ;

        const DBENGIN      = 'gb';
        const IMAGICKENGIN = 'imagick';

        public function __construct($fileName,$engine='gb')
        {
            if('gb'===$engine)
            {
                self::$_creator = self::initByGD($fileName);
            }
            if('imagick' === $engine)
            {
                self::$_creator = self::initByImagick($fileName);
            }
            else if(is_string($engine) && preg_match('/(.+\\.+)+/',$engine))
            {
               self::$_creator = new $engine($fileName);
            }
            else if($engine instanceof \Closure)
            {
                self::$_creator = $engine($fileName);
            }
            if(empty(self::$_creator))
            {
                $error = 'class init failed' ;
                $code  = ImageException::_code($error);
                throw  new ImageException(ImageException::createMsgByCode($code,__CLASS__),$code);
            }
        }

        public static function initByGD($filename)
        {
           return new ImageSDK($filename);
        }

        public static function initByImagick($filename)
        {
            return new ImagickSDK($filename);
        }

        public  function creator()
        {
            return self::$_creator;
        }
}